package com.wkbb.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.basic.constant.BasicConstants;
import com.wkbb.basic.feign.DoorFeignService;
import com.wkbb.basic.feign.WxFeignService;
import com.wkbb.basic.mapper.DeviceInfoMapper;
import com.wkbb.basic.mapper.OrganizationMapper;
import com.wkbb.basic.model.*;
import com.wkbb.basic.service.*;
import com.wkbb.basic.thread.SynDeviceTask;
import com.wkbb.basic.utils.WebankUtil;
import com.wkbb.basic.vo.DeviceConfigVo;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.business.BasicBusiness;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.RedisKeyConstant;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.DeviceInfoDto;
import com.wkbb.common.dto.basic.OrgServiceConfigDto;
import com.wkbb.common.dto.basic.OrganizationDto;
import com.wkbb.common.dto.wx.AccountDto;
import com.wkbb.common.enums.OrganizationTypeEnum;
import com.wkbb.common.redis.RedisTemplateUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.common.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Lilin
 */
@Service
@Slf4j
public class DeviceInfoServiceImpl extends BaseServiceImpl<DeviceInfo> implements DeviceInfoService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private WxFeignService wxFeignService;

    @Autowired
    @Lazy
    private XmppService xmppService;

    @Autowired
    private OrgServiceConfigService orgServiceConfigService;

    @Autowired
    private OrganizationServiceImpl organizationService;

    @Autowired
    private DoorFeignService doorFeignService;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private WebankUtilService webankUtilService;

    @Autowired
    private DeviceTypeService deviceTypeService;

    @Autowired
    private OrganizationWebankService organizationWebankService;
    
    @Autowired
    public void setMapper(DeviceInfoMapper mapper) {
        super.setMapper(mapper);
    }

    @Override
    public ResultDTO<Object> uploadDeviceInfo(String deviceCode, String softVer, String hardVer, String serial, Boolean webank, Boolean live, Boolean binocularOrMonocularCamera ,String apiVersion) throws Exception {
        DeviceInfo deviceInfo = getInfoByDeviceCode(deviceCode);
        if (deviceInfo == null) {
            return new ResultDTO<>(ErrorCode.DEVICE_NOT_EXISTS);
        }

        deviceInfo.setSoftwareVer(softVer);
        deviceInfo.setHardwareVer(hardVer);
        deviceInfo.setDeviceSerial(serial);
        deviceInfo.setUploadTime(new Date());
        super.update(deviceInfo);
        if(apiVersion.equals(VersionConstant.V1)) {//XMPP协议换成了MQTT协议
            redisTemplateUtil.set(BasicBusiness.getDeviceProtocalRedisKey(deviceCode),"mqtt");
        }else {
            redisTemplateUtil.delete(BasicBusiness.getDeviceProtocalRedisKey(deviceCode));
        }

        // 若该设备使用了微众的算法
        if (webank) {


//        log.info("--------开始查询学校微众appid--------orgId:{}",deviceInfo.getOrgId());
            OrganizationWebank organizationWebank = organizationWebankService.getOrganizationWebanksByOrgId(deviceInfo.getOrgId(),null);
            if(organizationWebank==null){
                log.info("--------获取学校微众appid结束--------查询webankappid为null,orgId:{}",deviceInfo.getOrgId());
                return null;
            }
            log.info("--------获取学校微众appid结束--------orgId:{}",deviceInfo.getOrgId());


            // 获取缓存信息
           /** WebankDeviceInfo webankDevice = (WebankDeviceInfo) redisTemplateUtil.get(getWebankDeviceRedisKey(deviceCode,deviceInfo.getOrgId()));
            // 判断当前设备信息是否和缓存的信息有区别
            if (!checkWebankDeviceDiff(webankDevice, deviceInfo, live)) {
                return new ResultDTO<>();
            }**/
            Integer cameraType = 1; //摄像头类型，0:红外双目 1:单目
            Integer cameraTypeSwitch = 0; //活体开关，0:关闭 1:开启 1) 如果摄像头类型是“红外双目”，则该开关设置的是“红外活体开关” 2) 如果摄像头类型是“1 单目”，则该开关是设置“翻拍活体开关” 3) 如果不传值，则默认红外活体开，翻拍活体关

            if(apiVersion.equals(VersionConstant.V2)){
                //新版本接口增加binocularOrMonocularCamera字段，为了兼容老版本的Android的设备，用接口版本号区分。
                //如果是老版本，cameraType和cameraTypeSwitch为null，不会上报给微众
               DeviceType deviceType = deviceTypeService.getInfo(deviceInfo.getDeviceTypeId());
               if(binocularOrMonocularCamera){//单目
                   cameraType = 1;
                   //单目，如果是云端识别，要打开翻拍开关
//                   if(null != deviceType && deviceType.getFaceStgy().intValue() ==  1){//人脸策略 1：云端 2：本地 3：混合 4：本地优先
//                       cameraTypeSwitch = 1;
//                   }
               }else{
                   cameraType = 0;
               }
                if(live){
                    cameraTypeSwitch = 1;
                }
           }else{
                cameraTypeSwitch = null;//老版本设备不上报这个参数，保持微众那边默认
                cameraType = null;
            }
            // 向微众报备设备
            String result = webankUtilService.addDevice(serial, deviceCode, live ? 1 : 0, deviceInfo.getName(), deviceInfo.getRemark(),
                    Collections.singletonList(deviceInfo.getOrgId()), false,organizationWebank.getWebankAppid(),organizationWebank.getWebankSecret(),
                    deviceInfo.getOrgId(),cameraType, cameraTypeSwitch);
            // 若设备已存在，则更新设备
            if (WebankUtil.deviceAlreadyExists(result)) {
                result = webankUtilService.modifyDevice(serial, deviceCode, live ? 1 : 0, deviceInfo.getName(), deviceInfo.getRemark(), "1",
                        "0", Collections.singletonList(deviceInfo.getOrgId()), false,organizationWebank.getWebankAppid(),organizationWebank.getWebankSecret(),
                        deviceInfo.getOrgId(),cameraType, cameraTypeSwitch);
                }

            // 如果操作成功，更新缓存,缓存1周
            boolean rs = WebankUtil.checkIfSuccess(result);
            if (rs) {
                WebankDeviceInfo webankDevice = new WebankDeviceInfo();
                BeanUtils.copyProperties(deviceInfo, webankDevice);
                webankDevice.setLive(live);
                redisTemplateUtil.set(getWebankDeviceRedisKey(deviceCode,deviceInfo.getOrgId()), webankDevice, RedisKeyConstant.EXPIRATION_TIME_1_WEEK);
            }
        }
        return new ResultDTO<>();
    }

    private String getWebankDeviceRedisKey(String deviceCode,Long orgId) {
        return RedisKeyConstant.WKBB_BASIC_WEBANK_DEVICE_INFO + deviceCode + ":"+"orgId:"+orgId;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO<Object> update(DeviceInfo oldDevice, DeviceInfo newDevice) throws Exception {
        DeviceInfo deviceInfo = deviceInfoMapper.getByPrimaryKey(newDevice.getId());
        super.update(newDevice);

        //先判断设备的orgid是否被改了
        if (oldDevice != null && oldDevice.getOrgId() != null && newDevice != null  && newDevice.getOrgId()!=null
                && newDevice.getOrgId().longValue()!=oldDevice.getOrgId().longValue()) {  //设备的orgid被改了
            log.info("修改设备参数，并且修改了设备的orgid，老orgid：{}，新orgid：{}",oldDevice.getOrgId(),newDevice.getOrgId());
            //先去微众删除设备
            OrganizationWebank organizationWebank = organizationWebankService.getOrganizationWebanksByOrgId(oldDevice.getOrgId(), null);

            if (null != organizationWebank && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankAppid()) && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankSecret())) {

                String result = webankUtilService.delDevice(oldDevice.getDeviceSerial(), false, organizationWebank.getWebankAppid(), organizationWebank.getWebankSecret(), oldDevice.getOrgId());
                boolean rs = WebankUtil.checkIfSuccess(result);
                log.info("修改设备参数，并且修改了设备的orgid。步骤1先去微众删除设备，result:{}",result);
                if(!rs){//去微众删除设备失败
                    newDevice.setOrgId(oldDevice.getOrgId());//把orgid改回去，不然用户以为修改成功
                    super.update(newDevice);
                    return new ResultDTO<>(ErrorCode.DEVICE_MODIFY_ORGID);
                }
            }

            //再去微众新增设备
            OrganizationWebank organizationWebank_new = organizationWebankService.getOrganizationWebanksByOrgId(newDevice.getOrgId(), null);

            if (null != organizationWebank_new && StringUtil.isNotEmptyIgnoreBlank(organizationWebank_new.getWebankAppid()) && StringUtil.isNotEmptyIgnoreBlank(organizationWebank_new.getWebankSecret())) {
                Integer cameraType = 1; //摄像头类型，0:红外双目 1:单目
                Integer cameraTypeSwitch = 0; //活体开关，0:关闭 1:开启 1) 如果摄像头类型是“红外双目”，则该开关设置的是“红外活体开关” 2) 如果摄像头类型是“1 单目”，则该开关是设置“翻拍活体开关” 3) 如果不传值，则默认红外活体开，翻拍活体关

                if(newDevice.getBinocularOrMonocularCamera()){//单目
                    cameraType = 1;
                    //单目，如果是云端识别，要打开翻拍开关
//                   if(null != deviceType && deviceType.getFaceStgy().intValue() ==  1){//人脸策略 1：云端 2：本地 3：混合 4：本地优先
//                       cameraTypeSwitch = 1;
//                   }
                }else{
                    cameraType = 0;
                }
                if(newDevice.getLive()){
                    cameraTypeSwitch = 1;
                }
                String result = webankUtilService.addDevice(newDevice.getDeviceSerial(), newDevice.getDeviceCode(), newDevice.getLive()? 1 : 0, newDevice.getName(),
                        newDevice.getRemark(), Collections.singletonList(newDevice.getOrgId()),
                        false, organizationWebank_new.getWebankAppid(), organizationWebank_new.getWebankSecret(),
                        newDevice.getOrgId(),cameraType,cameraTypeSwitch);
                log.info("修改设备参数，并且修改了设备的orgid。步骤2再去微众新增设备，result:{}",result);
                //假如微众新增失败，没有关系，设备还会自动上报
            }
        }

        int oldFaceRect = 0;
        int oldFaceSampling = 0;
        JSONObject oldConfig = JSON.parseObject(JSON.toJSONString(oldDevice));
        if (oldDevice != null && oldDevice.getFaceConfig() != null) {
            oldFaceRect = oldConfig.getInteger("faceRect") == null ? 0 : oldConfig.getInteger("faceRect");
            oldFaceSampling = oldConfig.getInteger("faceSampling") == null ? 0 : oldConfig.getInteger("faceSampling");
        }

        JSONObject newConfig = JSON.parseObject(JSON.toJSONString(newDevice));

        // 红外测温参数
        Boolean oldInfraredFlag = false;
        BigDecimal oldInfraredValue =new BigDecimal(0.00);
        BigDecimal oldInfraredTemperatureCompensation =new BigDecimal(0.00);

        if (oldDevice != null){
            oldDevice.setInfraredFlag(oldDevice.getInfraredFlag() == null ? false : oldDevice.getInfraredFlag());
        }

        if (oldDevice != null && oldDevice.getInfraredFlag() != null) {
            oldInfraredFlag = oldConfig.getBoolean("infraredFlag") == null ? false: oldConfig.getBoolean("infraredFlag");
            oldInfraredValue = oldConfig.getBigDecimal("infraredValue") == null ? new BigDecimal(0.00): oldConfig.getBigDecimal("infraredValue");
            oldInfraredTemperatureCompensation = oldConfig.getBigDecimal("infraredTemperatureCompensation") == null ? new BigDecimal(0.000): oldConfig.getBigDecimal("infraredTemperatureCompensation");

        }

        Boolean newInfraredFlag = false;
        BigDecimal newInfraredValue = new BigDecimal(0.00);
        BigDecimal newInfraredTemperatureCompensation = new BigDecimal(0.00);
        if (newDevice.getInfraredFlag() != null) {
            newInfraredFlag = newConfig.getBoolean("infraredFlag") == null ? false: newConfig.getBoolean("infraredFlag");
            newInfraredValue = newConfig.getBigDecimal("infraredValue") == null ? new BigDecimal(0.00): newConfig.getBigDecimal("infraredValue");
            newInfraredTemperatureCompensation = newConfig.getBigDecimal("infraredTemperatureCompensation") == null ? new BigDecimal(0.000): newConfig.getBigDecimal("infraredTemperatureCompensation");
        }

        //更新设备信息到门禁
        ThreadPoolUtil.addPoolTask(new SynDeviceTask(doorFeignService, newDevice.getOrgId(), deviceInfo.getDeviceCode(), newDevice.getDeviceCode(),
                newDevice.getName(), newDevice.getSoftwareVer(), newDevice.getScene(), false));


        if (!oldConfig.toJSONString().equals(newConfig.toJSONString())) {
            xmppService.configUpdate(null, newConfig.getString("deviceCode"));
        }


        if (!newInfraredFlag.equals(oldInfraredFlag) || newInfraredValue.compareTo(oldInfraredValue)==0 || newInfraredTemperatureCompensation.compareTo(oldInfraredTemperatureCompensation)==0) {
            xmppService.updateInfrared(null, newDevice.getDeviceCode(), newInfraredFlag, newInfraredValue, newInfraredTemperatureCompensation);
        }

        return new ResultDTO<>();
    }

    @Override
    public DeviceInfo getByPrimaryKey(Long id) throws Exception {
        DeviceInfo deviceInfo = super.getByPrimaryKey(id);
        if (deviceInfo == null) {
            return null;
        }

        deviceInfo.setFaceConfig(checkFaceConfig(deviceInfo.getFaceConfig()));
        return deviceInfo;
    }

    private Object checkFaceConfig(Object faceConfig) {
        JSONObject object;
        if (faceConfig == null) {
            object = new JSONObject();
            object.put("faceRange", getDefaultFaceRange());
            object.put("faceSimilar", BasicConstants.DEFAULT_FACE_CONFIG_SIMILAR);
            object.put("faceServer", BasicConstants.DEFAULT_FACE_CONFIG_SERVER);
            object.put("faceRect", BasicConstants.DEFAULT_FACE_CONFIG_RECT);
            object.put("faceSampling", BasicConstants.DEFAULT_FACE_CONFIG_SAMPLING);
        } else {
            object = (JSONObject) faceConfig;
            object.computeIfAbsent("faceRange", k -> getDefaultFaceRange());
            object.computeIfAbsent("faceSimilar", k -> BasicConstants.DEFAULT_FACE_CONFIG_SIMILAR);
            object.computeIfAbsent("faceServer", k -> BasicConstants.DEFAULT_FACE_CONFIG_SERVER);
            object.computeIfAbsent("faceRect", k -> BasicConstants.DEFAULT_FACE_CONFIG_RECT);
            object.computeIfAbsent("faceSampling", k -> BasicConstants.DEFAULT_FACE_CONFIG_SAMPLING);
        }

        int faceServer = object.getIntValue("faceServer");
        // 人脸识别服务，0 老优图 1 新优图
        object.put("faceServerUrl", faceServer == 0 ?
                BasicConstants.DEFAULT_FACE_CONFIG_SERVER_OLD :
                BasicConstants.DEFAULT_FACE_CONFIG_SERVER_NEW);

        return object;
    }

    /**
     * 设备默认人脸距离
     */
    private Object getDefaultFaceRange() {
        JSONObject object = new JSONObject();
        JSONObject far = new JSONObject();
        far.put("height", BasicConstants.DEFAULT_FACE_CONFIG_FAR);
        far.put("width", BasicConstants.DEFAULT_FACE_CONFIG_FAR);
        JSONObject near = new JSONObject();
        near.put("height", BasicConstants.DEFAULT_FACE_CONFIG_NEAR);
        near.put("width", BasicConstants.DEFAULT_FACE_CONFIG_NEAR);
        object.put("far", far);
        object.put("near", near);
        object.put("level", BasicConstants.DEFAULT_FACE_CONFIG_LEVEL);
        return object;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids, String deleteBy) throws Exception {
        if (ids.isEmpty()) {
            return;
        }

        for (Long id : ids) {
            if (id == null || id == 0) {
                return;
            }

            DeviceInfo deviceInfo1 = deviceInfoMapper.getByPrimaryKey(id);

            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setId(id);
            deviceInfo.setUpdateBy(deleteBy);
            deviceInfo.setStatus(false);
            update(deviceInfo);

            //删除设备缓存
             redisTemplateUtil.delete(getWebankDeviceRedisKey(deviceInfo1.getDeviceCode(),deviceInfo1.getOrgId()));

             //从微众删除设备
            try {
                OrganizationWebank organizationWebank = organizationWebankService.getOrganizationWebanksByOrgId(deviceInfo1.getOrgId(),null);
                if (null != organizationWebank && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankAppid()) && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankSecret())) {
                    webankUtilService.delDevice(deviceInfo1.getDeviceSerial(), false, organizationWebank.getWebankAppid(), organizationWebank.getWebankSecret(), deviceInfo1.getOrgId());
                    log.info("***********从微众删除设备...成功******webankAppid:{},deviceNo:{},deviceCode:{},orgId:{}", organizationWebank.getWebankAppid(), deviceInfo1.getDeviceSerial(), deviceInfo1.getDeviceCode(), deviceInfo1.getOrgId());
                }
            }catch (Exception e){
                log.error("***********从微众删除设备...失败******deviceNo:{},deviceCode:{},orgId:{}",  deviceInfo1.getDeviceSerial(), deviceInfo1.getDeviceCode(), deviceInfo1.getOrgId());
                e.printStackTrace();
            }

            //更新设备信息到门禁
            ThreadPoolUtil.addPoolTask(new SynDeviceTask(doorFeignService, deviceInfo1.getOrgId(), deviceInfo1.getDeviceCode(), null,
                    null, null, null, true));
        }
    }

    @Override
    public DeviceInfoDto getDtoByParams(Map<String, Object> params) {
        DeviceInfoDto deviceInfo = deviceInfoMapper.getDtoByParams(params);
        if (deviceInfo == null) {
            return null;
        }

        deviceInfo.setFaceConfig(checkFaceConfig(deviceInfo.getFaceConfig()));
        return deviceInfo;
    }

    @Override
    public List<DeviceInfoDto> getDtoListByParams(Map<String, Object> params) {
        return deviceInfoMapper.getDtoListByParams(params);
    }

    @Override
    public PageInfo<DeviceInfoDto> getDtoPageByParams(Map<String, Object> params, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return null;
        }
        Page<DeviceInfoDto> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<DeviceInfoDto> pageInfo = new PageInfo<>(deviceInfoMapper.getDtoListByParams(params));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public DeviceInfo getInfoByDeviceCode(String deviceCode) throws Exception {
        Map<String, Object> params = new HashMap<>(1);
        params.put("deviceCode", deviceCode);

        return getByMapParams(params);
    }

    @Override
    public DeviceConfigVo getConfigByDeviceCode(String deviceCode) {
        DeviceConfigVo config = deviceInfoMapper.getConfigByDeviceCode(deviceCode);
        if (config == null) {
            return null;
        }

        config.setFaceConfig(checkFaceConfig(config.getFaceConfig()));

        ResultDTO<AccountDto> accountByOrg = wxFeignService.getAccountByOrg(config.getOrgId());
        if (accountByOrg != null && accountByOrg.getData() != null) {
            config.setAccountId(accountByOrg.getData().getId());
        }

        OrganizationWebank organizationWebank = organizationWebankService.getOrganizationWebanksByOrgId(config.getOrgId(),null);

        if (null != organizationWebank && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankAppid()) && StringUtil.isNotEmptyIgnoreBlank(organizationWebank.getWebankSecret())) {

            //查询学校配置
            OrgServiceConfig orgServiceConfig = orgServiceConfigService.getOrgConfigByOrgId(config.getOrgId());
            judgeOrgType(orgServiceConfig);
            OrgServiceConfigDto orgServiceConfigDto = new OrgServiceConfigDto();
            BeanUtils.copyProperties(orgServiceConfig, orgServiceConfigDto);
            config.setOrgServiceConfigDto(orgServiceConfigDto);
            config.setWebankAppid(organizationWebank.getWebankAppid());
            config.setWebankSecret(organizationWebank.getWebankSecret());
        }

        return config;
    }


    /**
     * 根据学校类型判断特殊的全局配置(幼儿园取刷脸配置，k12的默认为可以)
     *
     * @param orgServiceConfig 配置信息
     */
    private void judgeOrgType(OrgServiceConfig orgServiceConfig) {
        try {
            OrganizationDto organizationDto = organizationService.getInfo(orgServiceConfig.getOrgId());
            if (organizationDto != null && !organizationDto.getOrgType().equals(OrganizationTypeEnum.KINDERGARTEN.getValue())) {
                orgServiceConfig.setPassFamily(true);
                orgServiceConfig.setPassStudent(true);
                orgServiceConfig.setPassClassTeacher(true);
            }
        } catch (Exception e) {
            log.info("获取学校信息异常，msg:{}", e);
            e.printStackTrace();
        }
    }

    @Override
    public PageInfo<DeviceInfoDto> getDeviceInfoListByParams(Map<String, Object> parms, Integer pageNo, Integer pageSize) throws Exception {

        if (pageNo == null || pageSize == null) {
            return null;
        }

        Page<DeviceInfoDto> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<DeviceInfoDto> pageInfo = new PageInfo<>(deviceInfoMapper.getDeviceInfoListByParams(parms));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public void updateDeviceConfigBatch(List<Long> ids, Integer level, Integer faceSimilar, Short attendanceSwitch, String updateName) {
        //组装数据
        Map<String, Object> parms = new HashMap<>(5);

        parms.put("list", ids);
        parms.put("level", level);
        parms.put("faceSimilar", faceSimilar);
        parms.put("attendanceSwitch", attendanceSwitch);
        parms.put("updateBy", updateName);

        deviceInfoMapper.updateDeviceConfigBatch(parms);

        for (Long id : ids) {
            DeviceInfo deviceInfo = deviceInfoMapper.getByPrimaryKey(id);
            if (deviceInfo != null) {
                if (level != null) {
                    xmppService.setLevel(null, deviceInfo.getDeviceCode(), level);
                }
                if (faceSimilar != null) {
                    xmppService.setSimilar(null, deviceInfo.getDeviceCode(), faceSimilar);
                }
                if (attendanceSwitch != null) {
//                    xmppService.setDoorEnable(null, deviceInfo.getDeviceCode(), attendanceSwitch.intValue());
                    //为了兼容老设备，attendanceSwitch参数值必须减1，因为Android端自己做了一个对应关系。2-》3,1-》2，0-》1
                    xmppService.setDoorEnable(null, deviceInfo.getDeviceCode(), attendanceSwitch.intValue()-1);
                }
            }
        }
    }

}
